package org.boyan.query;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.boyan.entity.query.ClusterTable;
import org.boyan.utils.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ExcelToClusterTable {

    //将传入的excel文件解析成集群表
    public static List<ClusterTable> getClusterTableList(Sheet sheet) {
        List<ClusterTable> clusterTableList = new ArrayList<>();
        try {
            int endRow = sheet.getLastRowNum();
            int startRow = beginIndex(sheet);
            if (startRow == -1) {
                throw new Exception("无法解析Excel,看下是不是出了什么问题");
            }
            for (int i = startRow + 1; i <= endRow; i++) {
                if ("".equals(sheet.getRow(i).getCell(5).getStringCellValue())) {
                    continue;
                }
                ClusterTable clusterTable = new ClusterTable();
                clusterTable.setColumnName(sheet.getRow(i).getCell(5).getStringCellValue());
                clusterTable.setColumnType(sheet.getRow(i).getCell(6).getStringCellValue());
                clusterTable.setColumnCnMeaning(sheet.getRow(i).getCell(7).getStringCellValue());
                clusterTableList.add(clusterTable);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clusterTableList;
    }


    //快速找到支撑集群接口表的字段名的行，找不到返回-1
    private static int beginIndex(Sheet sheet) {
        for (Row row : sheet) {
            Cell cell=row.getCell(5);
            if(cell==null){
                continue;
            }
            if ("字段名".equals(cell.getStringCellValue())) {
                return row.getRowNum();
            }
        }
        return -1;
    }
}
